package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Directory;
import cn.wolfcode.crm.mapper.DetailsMapper;
import cn.wolfcode.crm.mapper.DirectoryMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDirectoryService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class DirectoryServiceImpl implements IDirectoryService {
    @Autowired
    private DirectoryMapper mapper;
    @Autowired
    private DetailsMapper detailsMapper;

    public List<Directory> selectAll() {
        return mapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int count = mapper.query4Count(qo);
        if (count > 0) {
            return new PageResult(count, mapper.query4List(qo));
        }
        return new PageResult(count, Collections.EMPTY_LIST);
    }

    public void updateByPrimaryKey(Directory record) {
        mapper.updateByPrimaryKey(record);
    }


    public void insert(Directory record) {
        mapper.insert(record);
    }


    public Directory selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(Directory directory) {
        //删除字典目录之后要先把字典详细删除
        detailsMapper.deleteByDirectorySn(directory.getSn());
        //再删除字典目录
        mapper.deleteByPrimaryKey(directory.getId());
    }


}
